﻿using Ndns.Packet.Abstractions;
using System;

namespace Ndns.Cache
{
    struct DnsQueryKey : IEquatable<DnsQueryKey>
    {
        /// <summary>
        /// 表示DNS查询的名称。
        /// </summary>
        public DnsHostName HostName { get; }

        /// <summary>
        /// 表示DNS的查询类型。
        /// </summary>
        public DnsRecordType Type { get; }

        /// <summary>
        /// 表示DNS的类型。
        /// </summary>
        public DnsClass Class { get; }

        public DnsQueryKey(DnsHostName hostName, DnsRecordType t, DnsClass c)
        {
            HostName = hostName;
            Type = t;
            Class = c;
        }


        #region Equals,HashCode实现


        public bool Equals(DnsQueryKey other)
        {
            return HostName == other.HostName && Type == other.Type && Class == other.Class;
        }

        public override bool Equals(object obj)
        {
            if (obj is DnsQueryKey q)
                return Equals(q);
            return false;
        }

        public override int GetHashCode()
        {
            unchecked
            {
                int total = 17;
                total = total * 37 + HostName.GetHashCode();
                total = total * 37 + Type.GetHashCode() << 16 | Class.GetHashCode();
                return total;
            }
        }

        #endregion


    }
}
